package com.itlong.cloud.app.service.impl;

import com.itlong.cloud.POJO.VO.app.*;
import com.itlong.cloud.app.dao.IAppProjectAddressDao;
import com.itlong.cloud.app.service.IAppProjectAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



/**
 * <desc>
 *      APP端项目信息处理接口实现类
 * </desc>
 *
 * @createDate 2017/9/28
 */
@Service
public class AppProjectAddressServiceImpl implements IAppProjectAddressService {


    @Autowired
    IAppProjectAddressDao iAppProjectAddresstDao;

    /**
     *  <desc>
     *      获取所有国家信息列表,首字母分组及国家ID与国家名称
     *  </desc>
     * @return
     *  @author shengen.T
     * @createDate 2017/9/28
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public List<AppForeignProjectAddressVO> getAllCountryInitials() throws  Exception {

        List<AppForeignProjectAddressVO> appProjectAddressVOList = new ArrayList<>();
        List<Map<String, Object>> resultList = iAppProjectAddresstDao.getAllCountryInitialsList();
        if (resultList != null && resultList.size() > 0) {
            for (Map<String, Object> map : resultList) {
                AppForeignProjectAddressVO appProjectAddressVO = new AppForeignProjectAddressVO(map.get("initials").toString());
                List<AppCountryVO> appCountryVOList = new ArrayList<>();
                String addressIds = map.get("addressId").toString();
                String addressNames = map.get("addressName").toString();
                String[] addressIdArr = addressIds.split(",");
                String[] addressNameArr = addressNames.split(",");
                for (int j = 0; j < addressIdArr.length; j++){
                    AppCountryVO appCityVO = new AppCountryVO(addressIdArr[j],addressNameArr[j]);
                    appCountryVOList.add(appCityVO);
                }
                appProjectAddressVO.setCountrys(appCountryVOList);
                appProjectAddressVOList.add(appProjectAddressVO);
            }
        }
        return  appProjectAddressVOList;
    }

    /**
     * <desc>
     *      获取所有城市信息列表,首字母分组及城市ID与城市名称
     * </desc>
     *
     * @return
     * @author cuiduanwu
     * @createDate 2017-09-28
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public List<AppProjectAddressVO> getAllUsedCityInitials() throws  Exception {
        List<AppProjectAddressVO> appProjectAddressVOList = new ArrayList<>();
        List<Map<String, Object>> resultList = iAppProjectAddresstDao.getAllUsedInitialsList();
        if (resultList != null && resultList.size() > 0) {
            for (Map<String, Object> map : resultList) {
                AppProjectAddressVO appProjectAddressVO = new AppProjectAddressVO(map.get("initials").toString());
                List<AppCityVO> appCityVOList = new ArrayList<>();
                String addressIds = map.get("addressId").toString();
                String addressNames = map.get("addressName").toString();
                String parentAddressIds = map.get("parentAddressId").toString();
                String[] addressIdArr = addressIds.split(",");
                String[] addressNameArr = addressNames.split(",");
                String[] parentAddressIdArr = parentAddressIds.split(",");
                for (int j = 0; j < addressIdArr.length; j++){
                    AppCityVO appCityVO = new AppCityVO(addressIdArr[j],addressNameArr[j],parentAddressIdArr[j]);
                    appCityVOList.add(appCityVO);
                }
                appProjectAddressVO.setCitys(appCityVOList);
                appProjectAddressVOList.add(appProjectAddressVO);
            }
        }
        return  appProjectAddressVOList;
    }

    /**
     *  <desc>
     *      app2.0获取所有城市信息列表,首字母分组及城市ID与城市名称
     *  </desc>
     * @return
     *  @author Qiang.S
     * @createDate 2019/06/18
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public AppNewProjectAddressVO getNewAllUsedCityInitials() throws  Exception {
        AppNewProjectAddressVO appProjectAddressVO = new AppNewProjectAddressVO();
        appProjectAddressVO.setHotCitys(iAppProjectAddresstDao.getHotCitys());
        List<AppNewProjectAddressVO.Provinces> provinces = new ArrayList<>();
        List<Map<String, Object>> resultList = iAppProjectAddresstDao.getNewAllUsedInitialsList();
        if (resultList != null && resultList.size() > 0) {
            for (Map<String, Object> map : resultList) {
                AppNewProjectAddressVO.Provinces provinces1 = new AppNewProjectAddressVO.Provinces();
                provinces1.setInitial(map.get("initials").toString());
                provinces1.setProvince(map.get("province").toString());
                String addressId = map.get("addressId").toString();//省编号
                String addressIds = map.get("addressIds").toString();//城市ID
                String addressNames = map.get("addressNames").toString();//城市名称
                String initials = map.get("initialsAll").toString();//首字母
                String[] addressIdArr = addressIds.split(",");
                String[] addressNameArr = addressNames.split(",");
                String[] initialsArr = initials.split(",");
                List<AppNewProjectAddressVO.City> citys = new ArrayList<>();
                for (int j = 0; j < addressIdArr.length; j++){
                    AppNewProjectAddressVO.City city = new AppNewProjectAddressVO.City();
                    if ("110101".equals(addressIdArr[j]) || "120101".equals(addressIdArr[j]) || "310101".equals(addressIdArr[j])
                            || "500101".equals(addressIdArr[j]) || "810101".equals(addressIdArr[j]) || "820101".equals(addressIdArr[j])) {
                        citys = new ArrayList<>();
                        city.setCityId(addressId);
                        city.setInitial(map.get("initials").toString());
                        city.setName(map.get("province").toString());
                        citys.add(city);
                        break;
                    }
                    city.setCityId(addressIdArr[j]);
                    city.setInitial(initialsArr[j]);
                    city.setName(addressNameArr[j]);
                    citys.add(city);
                }
                provinces1.setCitys(citys);
                provinces.add(provinces1);
            }
        }
        appProjectAddressVO.setProvinces(provinces);
        return  appProjectAddressVO;
    }



}
